package DAO;

import java.util.List;











import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Beans.*;
import Controller.AdministradorMozo;
import Controller.AdministradorSalon;
import DTO.*;
import HibernateUtils.*;
import Model.Salon;



public class HibernateSalonDAO {
	
	private static HibernateSalonDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateSalonDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateSalonDAO();
		} 
		return instancia;
	}
	
	public void grabarSalon(SalonDTO s){
		/*Convierto el parametro a bean
		SalonBean aux = new SalonBean();
		aux.setNombre(s.getNombre());
		
		
		for(SectorDTO sec:s.getSectores()){
			SectorBean sbean=new SectorBean();
			sbean.setNombre(sec.getNombre());
			sbean.setMozos(AdministradorMozo.getInstancia().fromDTOTOBean(sec.getMozos()));
			
			for(MesaDTO m:sec.getMesas()){
				MesaBean mesa=new MesaBean();
				mesa.setCapacidad(m.getCapacidad());
				mesa.setNumero(m.getNumero());
				sbean.addMesa(mesa);
							
			}
			aux.addSector(sbean);
		
			
		}*/
		Salon sal=AdministradorSalon.getInstancia().fromDTOTOModel(s);
		SalonBean aux=AdministradorSalon.getInstancia().fromModelTOBean(sal);
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(aux);
		session.flush();
		session.getTransaction().commit();
		session.close();
		

}


	public void agregarSector(SalonDTO salon, SectorDTO s) {
		
		
		SalonBean sbean=new SalonBean();
		
		
		Session session = sf.openSession();
		session.load(sbean, 1);

		salon.agregarSector(s);
		
		session.beginTransaction();
		session.persist(sbean);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
		
	}

	public List<Object[]> listSalones() {
		Session session = sf.openSession();
		List<Object[]> list=session.createQuery("select sal, sectores from Salon sal join sal.sectores sectores").list();
		session.close();
		return list;
	}

	public SalonBean buscarSalon(SalonBean ab) {
		Session session=sf.openSession();
		SalonBean salon=(SalonBean) session.get(SalonBean.class,ab.getIdSalon());
		session.close();
		return salon;
	}

	
}
